package com.example.tms.mapper;

import com.example.tms.bean.OrderMgmt;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface OrderMapper extends JpaRepository<OrderMgmt,String>, JpaSpecificationExecutor<OrderMgmt> {

    //查询订单信息（ 订单id）
    @Query(value = "select * from order_mgmt where ddid = ?1 and sfcz = true", nativeQuery = true)
    OrderMgmt findByDdid(String ddid);

    //查询订单中所有路线信息
    @Query(value = "select distinct xlmc from order_mgmt where sfcz = true", nativeQuery = true)
    List<String> findRoadInOrder();

    //删除订单信息（假删除 订单id）
    @Query(value = "update order_mgmt set sfcz = false where ddid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void DeleteByDdid(String ddid);

    //运单退回
    @Query(value = "update order_mgmt set ddzt = '待发布' where ddh = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void waybillback(String ddh);

    //发布订单（订单id）
    @Query(value = "update order_mgmt set ddzt = '已发布' where ddid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void IssueByDdid(String ddid);

    //查询订单信息（订单日期 是否生成订单）
    List<OrderMgmt> findByDdrqAndSfscdd(String ddrq, boolean sfscdd);

    //查询当天订单数（订单日期）
    Long countByDdrq(String ddrq);

    OrderMgmt findByDdhAndSfcz(String ddh, boolean sfcz);

    //更新订单
    @Query(value = "update order_mgmt set huowu = ?2,hwsl=?3,ydj=?4,yunfei=?5,gls=?6 where ddid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void UpdateByDdid(String ddid ,String huowu,String hwsl,String ydj,String yunfei,String gls);

    OrderMgmt findByDdidAndSfcz(String ddid, boolean sfcz);

    //更新订单状态（回车登记后）
    @Query(value = "update order_mgmt set ddzt='已完成' where ddh = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void UpdateByDdidzt(String ddh);

    //更新订单状态(退回后)
    @Query(value = "update order_mgmt set ddzt='已发布' where ddh = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void UpdateByDdidthzt(String ddh);

    //更新订单状态(退回后)
    @Query(value = "select * from order_mgmt where str_to_date( ddrq , '%Y-%m-%d') < DATE_SUB(CURDATE(), INTERVAL 1 MONTH) and ddzt = ?1 ;", nativeQuery = true)
    List<OrderMgmt> findForDelete(String ddzt);

    //查询订单数量
    @Query(value = "select  count(ddid)  from order_mgmt where ddzt='已完成' and sfcz = true", nativeQuery = true)
    Integer findDdywc();

    //查询订单数量
    @Query(value = "select  count(ddid)  from order_mgmt where ddzt='已发布' and sfcz = true", nativeQuery = true)
    Integer findDdyfb();

    //查询订单数量
    @Query(value = "select  count(ddid)  from order_mgmt where ddzt='待发布' and sfcz = true", nativeQuery = true)
    Integer findDddfb();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 0 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd0dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 1 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd1dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 2 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd2dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 3 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd3dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 4 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd4dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 5 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd5dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 6 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd6dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 7 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd7dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 8 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd8dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 9 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd9dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 10 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd10dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 11 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd11dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 12 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd12dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 13 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd13dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 14 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd14dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 15 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd15dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 16 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd16dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 17 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd17dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 18 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd18dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 19 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd19dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 20 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd20dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 21 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd21dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 22 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd22dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 23 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd23dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 24 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd24dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 25 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd25dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 26 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd26dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 27 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd27dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 28 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd28dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 29 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd29dl();

    //查询订单数量
    @Query(value = "select count(ddid) from order_mgmt where TO_DAYS( NOW( ) ) - TO_DAYS( ddrq) = 30 and sfcz = true and ddzt !='待发布'", nativeQuery = true)
    Integer findDd30dl();
}
